<script>
var $, form, element, aceThemeName="aceTheme";
layui.use(['jquery', 'form', 'element'], function() {
	$ = layui.$, form = layui.form, element = layui.element;

	form.on('select(ace-theme)', function(data){
		changeAceTheme(data.value);
	});

	active = {
		newFile : function(){
			#dictList(parentKey="edit_theme_file_type")
			var extStr = "#for(dict : dictList)#if(for.index > 0),#end .#(dict.key)#end";
			#end
			layer.prompt({title: "请输入后缀为" + extStr + "的新文件名："}, function(value, index, elem){
				layer.close(index);
				var data = {dir: "#(dir)", file: value};
				$.ajax({
					url : "#(ctx)/manage/theme/edit/ajaxNewFile",
					type : "post",
					data : data, ### 提交数据
					beforeSend : function(){layer.load(1);}, ### 防止重复操作
					complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
					success : function(ret){
						if(ret.code == 0){
							layer.msg("创建成功！", {icon: 1});
							### 创建成功后，编辑新文件
							window.location.href="#(ctx)/manage/theme/edit?dir=#(dir)&file=" + data.file;
						} else {
							if(ret.msg){
								layer.alert(ret.msg, {icon: 0});
							} else {
								layer.alert("操作失败！", {icon: 2});
							}
						}
					},
					error : function (e) { ### Ajax调用失败
						layer.alert("操作失败：" + e.responseText, {icon: 2});
					}
				});
			});
		}
		, newDir : function(){
			layer.prompt({title: "请输入新文件夹名："}, function(value, index, elem){
				layer.close(index);
				var data = {dir: "#(dir)", file: value};
				$.ajax({
					url : "#(ctx)/manage/theme/edit/ajaxNewDir",
					type : "post",
					data : data, ### 提交数据
					beforeSend : function(){layer.load(1);}, ### 防止重复操作
					complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
					success : function(ret){
						if(ret.code == 0){
							layer.msg("创建成功！", {icon: 1});
							### 创建成功后，刷新页面
							window.location.href="#(ctx)/manage/theme/edit?dir=#(dir)";
						} else {
							if(ret.msg){
								layer.alert(ret.msg, {icon: 0});
							} else {
								layer.alert("操作失败！", {icon: 2});
							}
						}
					},
					error : function (e) { ### Ajax调用失败
						layer.alert("操作失败：" + e.responseText, {icon: 2});
					}
				});
			});
		}
		,delFile : function(file){
			var data = {dir: "#(dir)", file: file};
			layer.confirm('是否删除文件 [ ' + file + ' ] ?', function(index){
				layer.close(index);
				$.ajax({
					url : "#(ctx)/manage/theme/edit/ajaxDelFile",
					type : "post",
					data : data, ### 获取提交的数据
					beforeSend : function(){layer.load(1);}, ### 防止重复操作
					complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
					success : function(ret){
						if(ret.code == 0){
							layer.msg("文件删除成功！", {icon: 1});
							### 创建成功后，刷新当前文件夹
							window.location.href="#(ctx)/manage/theme/edit?dir=#(dir)";
						} else {
							if(ret.msg){
								layer.alert(ret.msg, {icon: 0});
							} else {
								layer.alert("操作失败！", {icon: 2});
							}
						}
					},
					error : function (e) { ### Ajax调用失败
						layer.alert("操作失败：" + e.responseText, {icon: 2});
					}
				});
			});
		}
		, delDir : function(file){
			var data = {dir: "#(dir)", file: file};
			layer.confirm('是否删除文件夹 [ ' + file + ' ] ?', function(index){
				layer.close(index);
				$.ajax({
					url : "#(ctx)/manage/theme/edit/ajaxDelDir",
					type : "post",
					data : data, ### 获取提交的数据
					beforeSend : function(){layer.load(1);}, ### 防止重复操作
					complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
					success : function(ret){
						if(ret.code == 0){
							layer.msg("文件夹删除成功！", {icon: 1});
							### 删除成功后，刷新当前页面
							window.location.href="#(ctx)/manage/theme/edit?dir=#(dir)";
						} else {
							if(ret.msg){
								layer.alert(ret.msg, {icon: 0});
							} else {
								layer.alert("操作失败！", {icon: 2});
							}
						}
					},
					error : function (e) { ### Ajax调用失败
						layer.alert("操作失败：" + e.responseText, {icon: 2});
					}
				});
			});
		}
		, saveFile : function(){
			if("#(file)" == "") return;
			var data = {dir: "#(dir)", file: "#(file)", fileContent : aceEditor.getValue()};
			layer.confirm('是否保存修改?', function(index){
				layer.close(index);
				$.ajax({ ### 用Ajax替换弹窗中的提交
					url : "#(ctx)/manage/theme/edit/ajaxSaveFile",
					type : "post",
					data : data, ### 获取提交的数据
					beforeSend : function(){layer.load(1);}, ### 防止重复操作
					complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
					success : function(ret){
						if(ret.code == 0){
							layer.msg("保存成功！", {icon: 1});
						} else {
							if(ret.msg){
								layer.alert(ret.msg, {icon: 0});
							} else {
								layer.alert("操作失败！", {icon: 2});
							}
						}
					},
					error : function (e) { ### Ajax调用失败
						layer.alert("操作失败：" + e.responseText, {icon: 2});
					}
				});
			});
		}
	}

    var aceTheme = Jfsa.getCookie(aceThemeName);
	if("#(file)" != "") {
        aceEditor = ace.edit("ID_editor");
        aceEditor.setTheme(aceTheme ? aceTheme : $('select[lay-filter="ace-theme"]').val());
        aceEditor.session.setMode("#(aceMode)");
        aceEditor.getSession().setTabSize(4);
        aceEditor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        window.aceEditor = aceEditor;
        $.ajax({
            url : "#(ctx)/manage/theme/edit/ajaxLoadFileContent",
            type : "post",
            data : {dir: '#(dir)', file:　'#(file)'}, ### 提交的数据
            beforeSend : function(){layer.load(1);}, ### 防止重复操作
            complete : function(){layer.closeAll('loading');}, ### 解除防重复操作
            success : function(ret){
            ### 处理后台的返回值，成功返回数据有：fileContent（文件内容）
                if(ret.code == 0){
                    aceEditor.setValue(ret.fileContent, -1);
                    aceLoaded = true;
                } else {
                    if(ret.msg){
                        layer.alert(ret.msg, {icon: 0}); ### Ajax调用成功，但后台返回失败信息
                    } else {
                        layer.alert("操作失败！", {icon: 2});
                    }
                }
            },
            error : function (e) { ### Ajax调用失败
                layer.alert("操作失败：" + e.responseText, {icon: 2});
            }
        });
    }

	function changeAceTheme(theme){
		if(!theme) return;
		Jfsa.setCookie(aceThemeName, theme, 999999);

		if(aceEditor){
			aceEditor.setTheme(theme);
			aceEditor.session.setMode("#(aceMode)");
		}

		$('select[lay-filter="ace-theme"]').val(theme);
		form.render('select');
	}

	changeAceTheme(aceTheme);
});
</script>
